package com.pdstudio.carrecom.ui.fragment.account;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lidroid.xutils.http.RequestParams;
import com.pdstudio.carrecom.R;
import com.pdstudio.carrecom.api.HttpExecuteJson;
import com.pdstudio.carrecom.api.ServiceHelper;
import com.pdstudio.carrecom.api.HttpExecuteJson.httpReturnJson;
import com.pdstudio.carrecom.app.UIHelper;
import com.pdstudio.carrecom.bean.LoginInfo;
import com.pdstudio.carrecom.tools.StringUtils;
import com.pdstudio.carrecom.ui.activity.account.ActivityGoRegister;

/**
 * 注册第二部
 * 类: FragmentRegisterS2 <br>
 * 描述: TODO <br>
 * 作者: xiangzhaoyu  <br>
 * 时间: 2015-5-18 上午11:22:55
 */
public class FragmentRegisterS2 extends Fragment {
	
	private View _view;
	private Activity _context;
	private Button mSubmit;
	private EditText mUsername,mPass,mPassag;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		_view = inflater.inflate(R.layout.fragment_account_register_s2, container,false);
		_context = getActivity();
		initialView();
		return _view;
	}
	
	private void initialView(){
		mSubmit=(Button)_view.findViewById(R.id.reg_submit);
		mSubmit.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(StringUtils.isEmpty(mUsername.getEditableText().toString())||StringUtils.isEmpty(mPass.getText().toString())){
					UIHelper.ToastMessage(_context, "用户名或者密码不能为空");
				}else{
					if(!mPass.getText().toString().equals(mPassag.getText().toString())){
						UIHelper.ToastMessage(_context, "两次输入的密码不一致");
					}else{
						try {
							mSubmit.setText("提交中...");
							ActivityGoRegister agr=(ActivityGoRegister)_context;
							
							HttpExecuteJson http = new HttpExecuteJson(_context, mLoginListener);
							RequestParams rps=new RequestParams();
							rps.addBodyParameter("carname", StringUtils.utf8Encode(agr.mCar, agr.mCar));
							rps.addBodyParameter("passwd", StringUtils.md5(mPass.getText().toString()));
							rps.addBodyParameter("name", mUsername.getText().toString());
							rps.addBodyParameter("phone",agr.mPhoneNum);
							http.post(ServiceHelper.Register, rps);
						} catch (Exception e) {
							e.printStackTrace();
							UIHelper.ToastMessage(_context, "注册失败："+e.getMessage());
						}
						
						
					}
				}
			}});
		mUsername=(EditText)_view.findViewById(R.id.reg_username);
		mPass=(EditText)_view.findViewById(R.id.reg_pass);
		mPassag=(EditText)_view.findViewById(R.id.reg_passag);
		
	}
	
	private void DoJson(String json){
		mSubmit.setText("提交");
		LoginInfo edv = null;
		try {
			edv = new Gson().fromJson(json,
					new TypeToken<LoginInfo>() {
					}.getType());
			if ((edv != null)
					&& (edv.msg.equals(ServiceHelper.msg_code_sucess))) {
				//原本在initview的代码放在这下面
				try{
					
					UIHelper.ToastMessage(_context, "注册账号成功，用户名为："+mUsername.getText().toString());
					_context.finish();
				}catch(Exception e){
					e.printStackTrace();
				}
			}else{
				UIHelper.ToastMessage(_context, "注册失败");
				
			}
		} catch (Exception e) {
			e.printStackTrace();
			UIHelper.ToastMessage(_context, "注册失败");
		}

	}
	
	private httpReturnJson mLoginListener = new httpReturnJson() {
		@Override
		public void onFailure(int error, String msg) {
			UIHelper.ToastMessage(_context, "注册失败" + msg);
		}

		@Override
		public void onCancel() {
			// TODO Auto-generated method stub
			UIHelper.ToastMessage(_context, "注册失败");
		}

		// 获取到饲料配方类型成功
		public void onSuccess(String result) {
			// TODO Auto-generated method stub
			// UIHelper.ToastMessage(_context,"总条数=" + result + "");
			DoJson(result);
		}
	};

	
}
